package com.example.eduservice.controller;


import com.example.eduservice.client.VodClient;
import com.example.eduservice.entity.EduVideo;
import com.example.eduservice.service.EduVideoService;
import com.example.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author xiuxian
 * @since 2022-06-26
 */
//@CrossOrigin  //跨域
@RestController
@RequestMapping("/eduservice/video")
public class EduVideoController {


    @Autowired
    private EduVideoService eduVideoService;

    //注入vodclient
    @Autowired
    private VodClient vodClient;

    //添加小节
    @PostMapping("/addVideo")
    public  R  addVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.save(eduVideo);

        return R.ok();
    }

    //删除小节
  //TODO  删除小节的时候将视频也删除了
    @DeleteMapping("/{id}")
    public  R  deleteVideo(@PathVariable String id){



        //根据小节id获取小节的全部信息
        EduVideo eduVideo = eduVideoService.getById(id);
        //获取小节的课程id
        String videoSourceId = eduVideo.getCourseId();
        //判断课程id是否为空
        if(!StringUtils.isEmpty(videoSourceId)){
            //删除改课程下的视频
            eduVideoService.removeVideoByid(videoSourceId);
        }

        //删除小节
        boolean result = eduVideoService.removeById(id);
        if(result){
            return  R.ok();
        }else{
            return  R.error();
        }

    }

    //修改小节
    @PostMapping("/updateVideo")
    public  R  updateVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.updateById(eduVideo);
        return  R.ok();
    }

    //根据id查询小节
    @GetMapping("/getVideo/{id}")
    public  R getVideo(@PathVariable String id){
        EduVideo eduVideo = eduVideoService.getById(id);
        return  R.ok().data("video",eduVideo);
    }




}

